#include <stdio.h>
#include <time.h>
#include "pal/protocol_rsu.h"
#include "t_types.h"
#include "common/lock.h"
#include "common/console.h"
#include "common/config.h"
#include "common/comm_tools.h"
#include "hal/hal_uart.h"
#include "userLog.h"
#include <string.h>
#include <stdlib.h>

#define PROTOCOL_RSU_RECV_BUF_LEN (1024 * 8)
#define PROTOCOL_RSU_SEND_BUF_LEN (1024 * 8)
#define PROTOCOL_RSU_INVALID_FRAME_SERIAL (0xff)

#define PROTOCOL_RSU_ENCODE_LEN(s32DataLen) (s32DataLen + 2)							  // frameserial + bcc
#define PROTOCOL_RSU_TOTAL_LEN(s32DataLen) (s32DataLen + sizeof(PROTOCOL_RSU_HEAD_S) + 2) // bcc + endflag

#define SWAP(x) ((((x)&0xf0) >> 4) | (((x)&0x0f) << 4))

T_INT32 PAL_PROTOCOL_RSU_PC_C0H_InitRsu(T_UINT8 u8RSCTL);
T_INT32 PAL_PROTOCOL_RSU_PC_C1H_ContinueBussiness(T_UINT8 u8RSCTL);
T_INT32 PAL_PROTOCOL_RSU_PC_C2H_StopBussiness(T_UINT8 u8RSCTL);

typedef struct _PROTOCOL_RSU_CONTEXT_S
{
	T_UINT8 au8RecvBuf[PROTOCOL_RSU_RECV_BUF_LEN];
	T_UINT8 au8SendBuf[PROTOCOL_RSU_SEND_BUF_LEN];
	T_UINT32 u32RecvPos;
	T_UINT32 u32SendPos;
	T_UINT8 u8PCFrameSerial;
} PROTOCOL_RSU_CONTEXT_S;

console_msg_declare(rsu);
PROTOCOL_RSU_CONTEXT_S *gpstRsuContext = NULL;
static T_INT32 gs32RsuUartPort = 0;

static T_UINT8 _PROTOCOL_RSU_GetPCFrameSerial(void)
{
	if (gpstRsuContext)
	{
		gpstRsuContext->u8PCFrameSerial++;
		if (gpstRsuContext->u8PCFrameSerial > 7)
		{
			gpstRsuContext->u8PCFrameSerial = 0;
		}
		gpstRsuContext->u8PCFrameSerial = gpstRsuContext->u8PCFrameSerial & 0x0f | 0x80;
		return gpstRsuContext->u8PCFrameSerial;
	}
	return 0;
}

static T_UINT8 _PROTOCOL_RSU_BCC(T_UINT8 *pu8Data, T_INT32 nLen)
{
	int i;
	T_UINT8 u8BCC = 0;

	for (i = 0; i < nLen; i++)
	{
		u8BCC ^= pu8Data[i];
	}

	return u8BCC;
}
static T_INT32 _PROTOCOL_RSU_Send(T_UINT8 *pu8Buf, T_INT32 nLen)
{
	if (gpstRsuContext == NULL)
	{
		LOG_ERROR("context not init");
		return -1;
	}

	return HAL_UART_Send(gs32RsuUartPort, pu8Buf, nLen);
}

static T_UINT32 _PROTOCOL_RSU_GetTimeSince2000(void)
{
	struct tm tm;
	time_t time2000;

	tm.tm_year = 2000 - 1900;
	tm.tm_mon = 0;
	tm.tm_mday = 1;
	tm.tm_hour = 0;
	tm.tm_min = 0;
	tm.tm_sec = 0;
	time2000 = mktime(&tm);
	return (time(NULL) - time2000);
}

static T_INT32 _RSU_CmdHandle(PROTOCOL_RSU_HEAD_S *pstHead)
{
	T_UINT8 u8FrameType = 0;
	T_INT32 s32CmdLen = 0;
	T_INT32 i;

	if (!pstHead)
	{
		error_null_ptr();
		return 0;
	}

	LOG_INFO("cmd type:0x%x", pstHead->au8Data[0]);

	switch (pstHead->au8Data[0])
	{
	case PROTOCOL_RSU_OBU_CMD_B0H_DEV_STATUS:
	{
		LOG_INFO("RSCTL=%02x", pstHead->u8FrameSerial);
		PROTOCOL_RSU_OBU_BOH_DEV_STATUS_S *pstDevStatus = (PROTOCOL_RSU_OBU_BOH_DEV_STATUS_S *)pstHead->au8Data;
		s32CmdLen = PROTOCOL_RSU_TOTAL_LEN(sizeof(PROTOCOL_RSU_OBU_BOH_DEV_STATUS_S));

#if 1
		PRINT_HEX(pstDevStatus->RsuStatus);
		PRINT_HEX(pstDevStatus->PSAMNum);
		LOG_INFO("pstDevStatus->GDTerminalId:");
		hzlog_info(userLog, pstDevStatus->GDTerminalId, 6);
		LOG_INFO("pstDevStatus->GBTerminalId:");
		hzlog_info(userLog, pstDevStatus->GBTerminalId, 6);
		PRINT_HEX(pstDevStatus->RsuAlgId);
		PRINT_HEX(pstDevStatus->RSUManuID);
		LOG_INFO("pstDevStatus->RSUIndividualID:");
		hzlog_info(userLog, pstDevStatus->RSUIndividualID, 3);
		LOG_INFO("pstDevStatus->Area_Decode:");
		hzlog_info(userLog, pstDevStatus->Area_Decode, 4);
		LOG_INFO("pstDevStatus->HardwareVersion:");
		hzlog_info(userLog, pstDevStatus->HardwareVersion, 2);
		LOG_INFO("pstDevStatus->SoftwareVersion:");
		hzlog_info(userLog, pstDevStatus->SoftwareVersion, 2);
		LOG_INFO("pstDevStatus->MakeDate:");
		hzlog_info(userLog, pstDevStatus->MakeDate, 4);
		PRINT_HEX(pstDevStatus->GBStatus);
		PRINT_HEX(pstDevStatus->GBMode);
		PRINT_HEX(pstDevStatus->WSMode);
#endif

		// init rsu
		if (pstHead->u8FrameSerial == 0x98)
		{
			PAL_PROTOCOL_RSU_PC_C0H_InitRsu(0x80);
		}
		// send c1 cmd
		else /*if(pstHead->u8FrameSerial == 0x08)*/
		{
			PAL_PROTOCOL_RSU_PC_C1H_ContinueBussiness(SWAP(pstHead->u8FrameSerial));
		}
	}
	break;

	case PROTOCOL_RSU_OBU_CMD_B2H_OBU_NUM:
	{
		PROTOCOL_RSU_OBU_B2H_OBU_NUM_S *pstObuNumber = (PROTOCOL_RSU_OBU_B2H_OBU_NUM_S *)pstHead->au8Data;
		s32CmdLen = PROTOCOL_RSU_TOTAL_LEN(sizeof(PROTOCOL_RSU_OBU_B2H_OBU_NUM_S));
		PRINT_HEX(pstObuNumber->OBUID);
		PRINT_INT(pstObuNumber->OBUStatus);
		PRINT_INT(pstObuNumber->OBUType);
		if (pstObuNumber->OBUStatus == 0x1) // normal
		{
			PAL_PROTOCOL_RSU_PC_C1H_ContinueBussiness(SWAP(pstHead->u8FrameSerial));
		}
		else if (pstObuNumber->OBUStatus == 0x2) // no ic card
		{
			PAL_PROTOCOL_RSU_PC_C2H_StopBussiness(SWAP(pstHead->u8FrameSerial));
		}
		else if (pstObuNumber->OBUStatus == 0x80) // heart beat
		{
			// nothing to do
		}
	}
	break;

	case PROTOCOL_RSU_OBU_CMD_B3H_OBU_PROPERTY:
	{
		PROTOCOL_RSU_OBU_B3H_OBU_PROPERTY_S *pstObuProperty = (PROTOCOL_RSU_OBU_B3H_OBU_PROPERTY_S *)pstHead->au8Data;
		s32CmdLen = PROTOCOL_RSU_TOTAL_LEN(sizeof(PROTOCOL_RSU_OBU_B3H_OBU_PROPERTY_S));
		PRINT_HEX(pstObuProperty->OBUID);
		PRINT_INT(pstObuProperty->ErrorCode);
		PRINT_INT(pstObuProperty->OBUType);
	}
	break;

	case PROTOCOL_RSU_OBU_CMD_B4H_IC_CARD_INFO:
	{
		PROTOCOL_RSU_OBU_B4H_IC_CARD_INFO_S *pstICCardInfo = (PROTOCOL_RSU_OBU_B4H_IC_CARD_INFO_S *)pstHead->au8Data;
		s32CmdLen = PROTOCOL_RSU_TOTAL_LEN(sizeof(PROTOCOL_RSU_OBU_B4H_IC_CARD_INFO_S));
		PRINT_HEX(pstICCardInfo->OBUID);
		PRINT_INT(pstICCardInfo->ErrorCode);
		PRINT_INT(pstICCardInfo->ICCType);
		// to do here ......
	}
	break;

	case PROTOCOL_RSU_OBU_CMD_B5H_BUSINESS_END:
	{
		PROTOCOL_RSU_OBU_B5H_BUSSINESS_END_S *pstBussinessEnd = (PROTOCOL_RSU_OBU_B5H_BUSSINESS_END_S *)pstHead->au8Data;
		s32CmdLen = PROTOCOL_RSU_TOTAL_LEN(sizeof(PROTOCOL_RSU_OBU_B5H_BUSSINESS_END_S));
		PRINT_HEX(pstBussinessEnd->OBUID);
		PRINT_INT(pstBussinessEnd->ErrorCode);
		PAL_PROTOCOL_RSU_PC_C1H_ContinueBussiness(SWAP(pstHead->u8FrameSerial));
	}
	break;

	case PROTOCOL_RSU_OBU_CMD_60H_PROCESS_STATUS:
	{
		PROTOCOL_RSU_OBU_60H_PROCESS_STATUS_S *pstProcessStatus = (PROTOCOL_RSU_OBU_60H_PROCESS_STATUS_S *)pstHead->au8Data;
		s32CmdLen = PROTOCOL_RSU_TOTAL_LEN(sizeof(PROTOCOL_RSU_OBU_60H_PROCESS_STATUS_S));
		LOG_INFO("card id:");
		for (i = 0; i < 8; i++)
		{
			LOG_INFO("%x ", pstProcessStatus->au8ICCardID[i]);
		}
		LOG_INFO("");
	}
	break;

	default:
	{
		LOG_ERROR("unknow rsu cmd:0x%x", pstHead->au8Data[0]);
		return 0;
	}
	break;
	}

	return s32CmdLen;
}

/*
>0:have one frame and return frame len
*/
static T_INT32 _CheckOneFrame(T_UINT8 *pu8Data, T_INT32 s32Len)
{
	T_INT32 i;

	if (!pu8Data || s32Len <= 0)
	{
		return -1;
	}

	if (s32Len < PROTOCOL_RSU_HEAD_LEN)
	{
		return 0;
	}

	if (pu8Data[0] != 0xff)
	{
		return 0;
	}

	for (i = 1; i < (s32Len); i++)
	{
		if (pu8Data[i] == 0xff)
		{
			return (i + 1);
		}
	}

	return 0;
}

#if 0
static T_INT32 _PROTOCOL_RSU_UartCb(void *pvParam)
{
	HAL_UART_CB_PARAM_S *pstCbParam = (HAL_UART_CB_PARAM_S*)pvParam;
	T_UINT8 *pu8Buff = NULL;
	T_INT32 s32DataLen = 0;
	T_INT32 s32Ret = 0;
	PROTOCOL_RSU_HEAD_S *pstHead;
	T_UINT8 au8EncodeBuff[PROTOCOL_RSU_RECV_BUF_LEN];
	T_UINT8 au8DecodeBuff[PROTOCOL_RSU_RECV_BUF_LEN];
	T_INT32 s32DecodeLen = 0;

	console_here;

	if(!gpstRsuContext)
	{
		return -1;
	}

	if(!pstCbParam)
	{
		error_null_ptr();
		return -1;
	}

	LOG_INFO("recv len:%d", pstCbParam->s32Len);

	memcpy(au8EncodeBuff, (void*)(pstCbParam->pu8Buff), pstCbParam->s32Len);
	COMM_SpecialBinStreamConvert_Decode(au8EncodeBuff,pstCbParam->s32Len,au8DecodeBuff,&s32DecodeLen);
	LOG_INFO("decode len:%d", s32DecodeLen);

	memcpy((void*)(gpstRsuContext->au8RecvBuf+gpstRsuContext->u32RecvPos), (void*)(au8DecodeBuff), s32DecodeLen);
	gpstRsuContext->u32RecvPos += s32DecodeLen;

	s32DataLen = gpstRsuContext->u32RecvPos;
	pu8Buff = gpstRsuContext->au8RecvBuf;

	LOG_INFO("rsu after decode hex:");
	hzlog_info(userLog,pu8Buff, s32DataLen);
	
	while(s32DataLen > PROTOCOL_RSU_HEAD_LEN)
	{
		s32Ret = _CheckOneFrame(pu8Buff, s32DataLen);
		// get one frame
		if(s32Ret > 0)
		{
			LOG_INFO("got one frame, len:%d", s32Ret);
			pstHead = (PROTOCOL_RSU_HEAD_S*)pu8Buff;
			_RSU_CmdHandle(pstHead);
			pu8Buff += s32Ret;
			s32DataLen -= s32Ret;
			continue;
		}
		pu8Buff++;
		s32DataLen--;
	}

	// move remain data
	if(s32DataLen > 0 && pu8Buff != gpstRsuContext->au8RecvBuf)
	{
		memcpy(gpstRsuContext->au8RecvBuf, pu8Buff, s32DataLen);
		gpstRsuContext->u32RecvPos = s32DataLen;
	}

	return 0;
}
#else
static T_INT32 _PROTOCOL_RSU_UartCb(void *pvParam)
{
	HAL_UART_CB_PARAM_S *pstCbParam = (HAL_UART_CB_PARAM_S *)pvParam;
	T_UINT8 *pu8Buff = NULL;
	T_INT32 s32SrcDataLen = 0;
	T_INT32 s32FrameLen = 0;
	T_INT32 s32Ret = 0;
	PROTOCOL_RSU_HEAD_S *pstHead;
	T_UINT8 au8EncodeBuff[PROTOCOL_RSU_RECV_BUF_LEN];
	T_UINT8 au8DecodeBuff[PROTOCOL_RSU_RECV_BUF_LEN];
	T_INT32 s32DecodeLen = 0;

	console_here;
	//return 0;

	if (!gpstRsuContext)
	{
		return -1;
	}

	if (!pstCbParam)
	{
		error_null_ptr();
		return -1;
	}

	LOG_INFO("recv len:%d", pstCbParam->s32Len);

	memcpy((void *)(gpstRsuContext->au8RecvBuf + gpstRsuContext->u32RecvPos), (void *)(pstCbParam->pu8Buff), pstCbParam->s32Len);
	gpstRsuContext->u32RecvPos += pstCbParam->s32Len;

	memcpy(au8EncodeBuff, (void *)(gpstRsuContext->au8RecvBuf), gpstRsuContext->u32RecvPos);
	s32SrcDataLen = gpstRsuContext->u32RecvPos;
	pu8Buff = gpstRsuContext->au8RecvBuf;

	LOG_INFO("s32SrcDataLen0000000:%d", s32SrcDataLen);
	while (s32SrcDataLen > PROTOCOL_RSU_HEAD_LEN)
	{
		s32FrameLen = _CheckOneFrame(pu8Buff, s32SrcDataLen);
		// get one frame
		if (s32FrameLen > 0)
		{
			LOG_INFO("got one frame, len:%d", s32FrameLen);
			COMM_SpecialBinStreamConvert_Decode(pu8Buff, s32FrameLen, au8DecodeBuff, &s32DecodeLen);
			LOG_INFO("rsu after decode len:%d, hex:", s32DecodeLen);
			hzlog_info(userLog, au8DecodeBuff, s32DecodeLen);
			pstHead = (PROTOCOL_RSU_HEAD_S *)au8DecodeBuff;
			_RSU_CmdHandle(pstHead);
			pu8Buff += s32FrameLen;
			s32SrcDataLen -= s32FrameLen;
			continue;
		}
		//pu8Buff++;
		//s32SrcDataLen--;
		//LOG_INFO("s32SrcDataLen:%d", s32SrcDataLen);
	}

	// move remain data
	if (s32SrcDataLen > 0 && pu8Buff != gpstRsuContext->au8RecvBuf)
	{
		memcpy(au8EncodeBuff, pu8Buff, s32SrcDataLen);
		memset(gpstRsuContext->au8RecvBuf, 0, sizeof(gpstRsuContext->au8RecvBuf));
		memcpy(gpstRsuContext->au8RecvBuf, au8EncodeBuff, s32SrcDataLen);
		gpstRsuContext->u32RecvPos = s32SrcDataLen;
	}
	else
	{
		memset(gpstRsuContext->au8RecvBuf, 0, sizeof(gpstRsuContext->au8RecvBuf));
		gpstRsuContext->u32RecvPos = 0;
	}

	return 0;
}
#endif

static T_INT32 _PROTOCOL_RSU_PC_SendCmd(T_UINT8 *pData, T_INT32 s32Len, T_UINT8 u8FrameSerial)
{
	T_UINT8 au8Buff[1024 * 2];
	T_UINT8 au8BuffEncode[1024 * 4];
	T_UINT8 *pByte = NULL;
	T_INT32 s32LenEncode = 0;
	T_INT32 s32Ret = 0;

	PROTOCOL_RSU_HEAD_S *pHead = (PROTOCOL_RSU_HEAD_S *)au8Buff;

	pHead->u8FrameStartFlag = 0xFF;
	if (u8FrameSerial == PROTOCOL_RSU_INVALID_FRAME_SERIAL)
	{
		pHead->u8FrameSerial = _PROTOCOL_RSU_GetPCFrameSerial();
	}
	else
	{
		pHead->u8FrameSerial = u8FrameSerial;
	}
	memcpy(pHead->au8Data, pData, s32Len);

	pByte = (T_UINT8 *)(pHead->au8Data + s32Len);
	*pByte = _PROTOCOL_RSU_BCC((T_UINT8 *)&pHead->u8FrameSerial, s32Len + 1);
	pByte++;
	*pByte = 0xFF;

	au8BuffEncode[0] = 0xff;
	s32Ret = COMM_SpecialBinStreamConvert_Encode((T_UINT8 *)&pHead->u8FrameSerial, PROTOCOL_RSU_ENCODE_LEN(s32Len), (T_UINT8 *)&au8BuffEncode[1], &s32LenEncode);
	if (s32Ret != 0)
	{
		LOG_ERROR("encode data error");
		return -1;
	}
	pByte = au8BuffEncode + s32LenEncode + 1;
	*pByte = 0xff;

	LOG_INFO("Send cmd:");
	hzlog_info(userLog, pHead, s32LenEncode + 2);
	return _PROTOCOL_RSU_Send((T_UINT8 *)pHead, s32LenEncode + 2);
}

/*��RSU�ؼ��������г�ʼ��/���ã�������RSU������*/
T_INT32 PAL_PROTOCOL_RSU_PC_C0H_InitRsu(T_UINT8 u8RSCTL)
{
	PROTOCOL_RSU_PC_C0H_INIT_S stInit;

	console_here;

	stInit.CMDType = PROTOCOL_RSU_PC_CMD_C0H_INIT;
	COMM_GetDateTimeBCD6(stInit.Datetime);
	//stInit.Seconds = COMM_HostToNetL(_PROTOCOL_RSU_GetTimeSince2000());
	stInit.Seconds = _PROTOCOL_RSU_GetTimeSince2000();
	PRINT_HEX(stInit.Seconds);
	PRINT_INT(stInit.Seconds);
	stInit.LaneMode = 0x02;
	stInit.WaitTime = 1;
	stInit.TxPower = 7;
	stInit.PLLChannelID = 1;
	stInit.GBMode = 3;
	stInit.WSMode = 0;
	//	return _PROTOCOL_RSU_PC_SendCmd((T_UINT8 *)&stInit, sizeof(PROTOCOL_RSU_PC_C0H_INIT_S), 0x89);
	return _PROTOCOL_RSU_PC_SendCmd((T_UINT8 *)&stInit, sizeof(PROTOCOL_RSU_PC_C0H_INIT_S), u8RSCTL);
}

T_INT32 PAL_PROTOCOL_RSU_PC_C1H_ContinueBussiness(T_UINT8 u8RSCTL)
{
	PROTOCOL_RSU_PC_C1H_CONTINUE_BUSINESS_S stContinueBussiness;
	console_here;

	stContinueBussiness.CMDType = PROTOCOL_RSU_PC_CMD_C1H_CONTINUE_BUSINESS;
	stContinueBussiness.OBUID = 0x0; // ��ʼ��ʱ�� OBU ID �� 0x00 0x00 0x00 0x00

	//return _PROTOCOL_RSU_PC_SendCmd((T_UINT8 *)&stContinueBussiness, sizeof(PROTOCOL_RSU_PC_C1H_CONTINUE_BUSINESS_S), PROTOCOL_RSU_INVALID_FRAME_SERIAL);
	return _PROTOCOL_RSU_PC_SendCmd((T_UINT8 *)&stContinueBussiness, sizeof(PROTOCOL_RSU_PC_C1H_CONTINUE_BUSINESS_S), u8RSCTL);
}

T_INT32 PAL_PROTOCOL_RSU_PC_C2H_StopBussiness(T_UINT8 u8RSCTL)
{
	PROTOCOL_RSU_PC_C2H_STOP_BUSINESS_S stStopBussiness;

	stStopBussiness.CMDType = PROTOCOL_RSU_PC_CMD_C1H_CONTINUE_BUSINESS;
	stStopBussiness.OBUID = 0x0; // ????????

	//	return _PROTOCOL_RSU_PC_SendCmd((T_UINT8 *)&stStopBussiness, sizeof(PROTOCOL_RSU_PC_C2H_STOP_BUSINESS_S), PROTOCOL_RSU_INVALID_FRAME_SERIAL);
	return _PROTOCOL_RSU_PC_SendCmd((T_UINT8 *)&stStopBussiness, sizeof(PROTOCOL_RSU_PC_C2H_STOP_BUSINESS_S), u8RSCTL);
}

/*
** 0->off,1->Զ������,2->��������
*/
T_INT32 PAL_PROTOCOL_RSU_PC_4CH_RfOnoff(T_INT32 s32On)
{
	PROTOCOL_RSU_PC_4CH_RF_ONOFF_S stRfOnff;

	stRfOnff.CMDType = PROTOCOL_RSU_PC_CMD_4CH_RF_ONOFF;
	stRfOnff.ControlType = s32On;

	return _PROTOCOL_RSU_PC_SendCmd((T_UINT8 *)&stRfOnff, sizeof(PROTOCOL_RSU_PC_4CH_RF_ONOFF_S), PROTOCOL_RSU_INVALID_FRAME_SERIAL);
}
static int _CMD_RSU_RFOnoff(CONSOLE_CMD_PARAM_S *pParam, char *pAck)
{
	int s32Param = 0;
	char abyHelp[] = "./cmd param. param:0->off,1->remote area, 2->close area";

	if (pParam->nArgsNum != 1)
	{
		sprintf(pAck, "%s\n", abyHelp);
		return 0;
	}

	s32Param = atoi(pParam->abyArgs[0]);
	PAL_PROTOCOL_RSU_PC_4CH_RfOnoff(s32Param);
	sprintf(pAck, "ok\n");
	return 0;
}
static int _CMD_RSU_InitCmd(CONSOLE_CMD_PARAM_S *pParam, char *pAck)
{
	int s32Param = 0;
	char abyHelp[] = "./cmd";

	PAL_PROTOCOL_RSU_PC_C0H_InitRsu(0x89);
	sprintf(pAck, "ok\n");
	return 0;
}
static int _CMD_RSU_C1(CONSOLE_CMD_PARAM_S *pParam, char *pAck)
{
	int s32Param = 0;
	char abyHelp[] = "./cmd";

	PAL_PROTOCOL_RSU_PC_C1H_ContinueBussiness(0x80);
	sprintf(pAck, "ok\n");
	return 0;
}

T_INT32 PAL_PROTOCOL_RSU_Init(void)
{
	T_INT32 s32Ret = 0;

	console_level(rsu, 0);

	gpstRsuContext = (PROTOCOL_RSU_CONTEXT_S *)malloc(sizeof(PROTOCOL_RSU_CONTEXT_S));
	if (!gpstRsuContext)
	{
		LOG_ERROR("can not alloc memory for rsu context");
		return -1;
	}
	memset((void *)gpstRsuContext, 0, sizeof(PROTOCOL_RSU_CONTEXT_S));

	s32Ret = HAL_UART_Probe(HAL_UART_APP_NAME_RSU);
	if (s32Ret < 0)
	{
		LOG_ERROR("HAL_UART_Probe() error");
		return -1;
	}
	gs32RsuUartPort = s32Ret;
	LOG_INFO("rsu Uart in chn:%d", gs32RsuUartPort);

#if 1
	s32Ret = HAL_UART_RegisterRecvCallback(gs32RsuUartPort, _PROTOCOL_RSU_UartCb);
	if (s32Ret < 0)
	{
		LOG_ERROR("HAL_UART_RegisterRecvCallback() _PROTOCOL_RSU_UartCb() error");
		return -1;
	}
#endif

	Console_CmdAdd("t-pal-rsu-rfonoff", _CMD_RSU_RFOnoff);
	Console_CmdAdd("t-pal-rsu-cmd-c0", _CMD_RSU_InitCmd);
	Console_CmdAdd("t-pal-rsu-cmd-c1", _CMD_RSU_C1);

	return 0;
}

T_INT32 PAL_PROTOCOL_RSU_DeInit(void)
{
	if (gpstRsuContext)
	{
		free((void *)gpstRsuContext);
	}
	return 0;
}
